package com.code.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.code.springboot.common.R;
import com.code.springboot.dto.DishDto;
import com.code.springboot.dto.SetmealDto;
import com.code.springboot.entity.Category;
import com.code.springboot.entity.Dish;
import com.code.springboot.entity.Setmeal;
import com.code.springboot.service.CategoryService;
import com.code.springboot.service.SetmealDishService;
import com.code.springboot.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: springboot
 * @create: 2022-08-10 11:52
 **/
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;


    @Autowired
    private CategoryService categoryService;

    //套餐分页查询
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){


//        //构造分页构造器
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();
//        //构造条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();

//        //添加过滤条件
        queryWrapper.like(name != null,Setmeal::getName,name);

//
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
//
//        //执行查询
        setmealService.page(pageInfo,queryWrapper);
//
//        //对象拷贝,records不拷贝
        BeanUtils.copyProperties(pageInfo,setmealDtoPage,"records");
//
//
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list = records.stream().map((item)->{
//
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
//
            Long categoryId = item.getCategoryId();
//
            Category category = categoryService.getById(categoryId);
            if(category != null){
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(list);
       return R.success(setmealDtoPage);
    }

    //新增套餐
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){

        setmealService.saveWithDish(setmealDto);

        return R.success("新增菜单成功");
    }


    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);
        return R.success("删除成功");
    }

    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable int status,@RequestParam List<Long> ids){
        setmealService.updateStatus(status,ids);
        return R.success("状态修改成功");
    }

//根据条件查套餐
    @GetMapping("/list")
    public R<List<Setmeal>> list( Setmeal setmeal){
        //构造查询条件
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        //查询状态为1的菜品

        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());

        //添加排序条件
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);

        return R.success(list);
    }


}
